package com.xc.controller;

import com.xc.request.CartItemRequest;
import com.xc.service.CartService;
import com.xc.utils.JsonData;
import com.xc.vo.CartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author xingchen
 * @description 购物车相关功能
 * @create 2021/11/23 21:24
 */
@Api(tags = "购物车")
@RestController
@RequestMapping("/api/cart/v1/")
public class CartController {

    @Autowired
    private CartService cartService;

    @ApiOperation("添加到购物车")
    @PostMapping("add")
    public JsonData addCart(@ApiParam("购物项") @RequestBody CartItemRequest cartItemRequest) {

        cartService.addToCart(cartItemRequest);

        return JsonData.buildSuccess();
    }

    @ApiOperation("清空购物车")
    @DeleteMapping("clear")
    public JsonData clearMyCart() {

        cartService.clear();
        return JsonData.buildSuccess();
    }

    @ApiOperation("查看我的购物车")
    @GetMapping("mycart")
    public JsonData findMyCart() {

        CartVO cartVO = cartService.getMyCart();
        return JsonData.buildSuccess(cartVO);
    }

    @ApiOperation("删除购物项")
    @DeleteMapping("delete/{product_id}")
    public JsonData deleteItem(@ApiParam(value = "商品id", required = true) @PathVariable("product_id") Long productId) {

        cartService.deleteItem(productId);
        return JsonData.buildSuccess();
    }

    @ApiOperation("修改购物车购物项数量")
    @PostMapping("change")
    public JsonData changeItem(@ApiParam("购物项") @RequestBody CartItemRequest cartItemRequest) {

        cartService.changeItemNum(cartItemRequest);

        return JsonData.buildSuccess();
    }
}
